import hashlib
from flask import Blueprint, render_template, request, url_for, redirect
from apps.user.model import User
from exts import db


user_bp = Blueprint('user', __name__) # 蓝图用来挂载路由路径

@user_bp.route('/', endpoint='home')  #可以使用{{ url_for('user.home') }} 指向这个路由
def user_center():
    # 进入路由前先查询数据在通报结果
    # 查询数据库数据
    users = User.query.filter(User.isdelete == False).all()  # select * from user;
    return render_template('user/show.html', users=users)  # 把查询到的数据传到页面

@user_bp.route('/register', methods=['GET', 'POST'])
def register():
    if request.method == 'POST':
        username = request.form.get('username')
        password = request.form.get('password')
        repassword = request.form.get('repassword')
        email = request.form.get('email')
        if password == repassword:
            # 与模型结合
            user = User()  # 找到模型类创建实例对象
            user.username = username  # 将网页传递的数据填入模型实例
            # 传统加密写法
            user.password = hashlib.sha256(password.encode('utf-8')).hexdigest()
            user.email = email
            # 使用数据库工具把实例化的数据上传到数据库
            db.session.add(user)  # 添加到缓冲区
            db.session.commit()  # 提交缓冲数据
            return redirect(url_for('user.home'))
        else:
            return '两次密码不一致！'
    return render_template('user/register.html')

@user_bp.route('/login', methods=['GET', 'POST'])
def login():
    if request.method == 'POST':
        username = request.form.get('username')
        password = request.form.get('password')
        # 关键 select * from user where username= ‘username’
        new_password = hashlib.sha256(password.encode('utf-8')).hexdigest()
        user_list = User.query.filter_by(username=username)
        for u in user_list:  # user_list这里返回可能是多个同用户名的对象列表，除非你的数据库不允许重名
            if u.password == new_password:
                return render_template('user/login.html', msg='登录成功')
            else:
                return render_template('user/login.html', msg='密码或用户名不对')
    return render_template('user/login.html')


@user_bp.route('/logout', methods=['GET', 'POST'])
def logout():
    return '用户退出'


@user_bp.route('/search', methods=['GET', 'POST'])
def search():
    if request.method == 'GET':
        keyword = request.args.get('search')
        user_list = User.query.filter(User.username.contains(keyword)).all()
        return render_template('user/show.html', users=user_list)


@user_bp.route('/del', methods=['GET', 'POST'])
def del_user():
    if request.method == 'GET':
        id = request.args.get('id')
        user = User.query.get(id)

        # 逻辑删除
        # user.isdelete = True
        # db.session.commit()

        # 物理删除
        db.session.delete(user)
        db.session.commit()
    return redirect(url_for('user.home'))


@user_bp.route('/update', methods=['GET', 'POST'])
def update_user():
    if request.method == 'POST':
        username = request.form.get('username')
        email = request.form.get('email')
        id = request.form.get('id')
        # 改用户信息
        user = User.query.get(id)
        user.username = username
        user.email = email
        # 提交
        db.session.commit()
        return redirect(url_for('user.home'))

    else:
        id = request.args.get('id')
        user = User.query.get(id)
        print(user)
        return render_template('user/update.html', user=user)
